package com.dzb.langchain4j.controller;

import com.dzb.langchain4j.common.config.AiConfig;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.service.TokenStream;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.LocalDate;

/**
 * 1.
 *
 * @ClassName ChatController
 * @Description
 * @Author 418167
 * @Date 2025/07/22/20:07
 */
@Slf4j
@Tag(name = "gscsd")
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    ChatLanguageModel qwenChatModel;

    @Operation(summary = "对话-/")
    @GetMapping("/qwenChat/{id}/{message}")
    public String qwenChat(@PathVariable String id
            , @PathVariable String message
    ) {
        return qwenChatModel.chat(message);
    }

    @Operation(summary = "对话-?")
    @GetMapping(value = "/chat")
    public String chat(@RequestParam(value = "message"
            , defaultValue = "你是谁") String message) {
        return qwenChatModel.chat(message);
    }

    @Autowired
    StreamingChatLanguageModel qwenStreamingChatModel;

    @Operation(summary = "流式对话-get")
    @RequestMapping(value = "/chatStream", produces = "text/stream;charset=UTF-8", method = RequestMethod.GET)
    public Flux<String> chatStream(@RequestParam(value = "message"
            , defaultValue = "你是谁") String message) {

        return Flux.create(sink -> {
            qwenStreamingChatModel.chat(message, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String s) {
                    log.info("===>>>ai_response: {}", s);
                    sink.next(s); // 逐次返回部分响应
                }

                @Override
                public void onCompleteResponse(ChatResponse chatResponse) {
                    sink.complete(); // 完成整个响应流
                }

                @Override
                public void onError(Throwable throwable) {
                    sink.error(throwable); // 异常处理
                }
            });
        });
    }

    @Autowired
    AiConfig.AssistantAi assistant;

    @Operation(summary = "普通对话-aiConfig")
    @GetMapping(value = "/chatConfig")
    public String chatConfig(@RequestParam(value = "message", defaultValue = "你是谁") String message) {
        return assistant.chat(message);
    }

    @Operation(summary = "流式对话-aiConfigStream")
    @GetMapping(value = "/chatConfigStream", produces = "text/stream;charset=UTF-8")
    public Flux<String> chatConfigStream(
            @RequestParam(value = "message", defaultValue = "你是谁") String message) {
        TokenStream stream = assistant.chatStream(message);

        return Flux.create(sink -> {
            stream.onPartialResponse(s -> {
                        sink.next(s);
                        log.info("===>>>AiConfig.AssistantAi.chatConfigStream response: {}", s);
                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();
        });
    }

    @Autowired
    AiConfig.AssistantAiUnique assistantUnique;

    @Operation(summary = "内存隔离-普通对话-aiConfigChatUnique")
    @GetMapping(value = "/chatConfigChatUnique")
    public String chatConfigChatUnique(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int userId
    ) {
        String chat = assistantUnique.chat(userId, message);
        return chat;
    }

    @Operation(summary = "内存隔离-流式对话-aiConfigChatStreamUnique")
    @GetMapping(value = "/chatConfigChatStreamUnique", produces = "text/stream;charset=UTF-8")
    public Flux<String> chatConfigChatStreamUnique(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int userId
    ) {
        TokenStream tokenStream = assistantUnique.chatStream(userId, message);

        return Flux.create(sink -> {
            tokenStream.onPartialResponse(s -> {
                        sink.next(s);
                        log.info("===>>>AiConfig.AssistantAi.chatConfigChatStreamUnique response: {}", s);
                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();
        });
    }

    @Autowired
    AiConfig.AssistantAiUniqueSql assistantUniqueSql;

    @Operation(summary = "mongodb 隔离-普通对话-aiConfigChatUniqueSql")
    @GetMapping(value = "/chatConfigChatUniqueSql")
    public String chatConfigChatUniqueSql(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int memoryId
    ) {
        return assistantUniqueSql.chat(memoryId, message);
    }

    @Operation(summary = "mongodb 隔离-流式对话-aiConfigChatStreamUniqueSql")
    @GetMapping(value = "/chatConfigChatStreamUniqueSql", produces = "text/stream;charset=UTF-8")
    public Flux<String> chatConfigChatStreamUniqueSql(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int memoryId
    ) {
        TokenStream tokenStream = assistantUniqueSql.chatStream(memoryId, message);
        Flux<String> returnFlux = Flux.create(sink -> {
            tokenStream.onPartialResponse(s -> {
                        sink.next(s);
                        log.info("===>>>AiConfig.AssistantAi.chatConfigChatStreamUniqueSql response: {}", s);
                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();
        });
        return returnFlux;
    }


    @Autowired
    AiConfig.AssistantAiUniqueSqlSysMgs assistantAiUniqueSqlSysMgs;

    @Operation(summary = "mongodb 隔离-普通对话-sys对话提示-chatConfigChatUniqueSqlSysMgs")
    @GetMapping(value = "/chatConfigChatUniqueSqlSysMgs")
    public String chatConfigChatUniqueSqlSysMgs(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int memoryId
    ) {
        return assistantAiUniqueSqlSysMgs.chat(memoryId, message);
    }

    @Operation(summary = "mongodb 隔离-流式对话-sys对话提示-chatConfigChatStreamUniqueSqlSysMgs")
    @GetMapping(value = "/chatConfigChatStreamUniqueSqlSysMgs", produces = "text/stream;charset=UTF-8")
    public Flux<String> chatConfigChatStreamUniqueSqlSysMgs(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int memoryId
    ) {
        TokenStream tokenStream = assistantAiUniqueSqlSysMgs.chatStream(memoryId, message, LocalDate.now().toString());
        Flux<String> returnFlux = Flux.create(sink -> {
            tokenStream.onPartialResponse(s -> {
                        sink.next(s);
                        log.info("===>>>AiConfig.AssistantAi.chatConfigChatStreamUniqueSqlSysMgs response: {}", s);
                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();
        });
        return returnFlux;
    }

    @Autowired
    AiConfig.AssistantAiUniqueSqlTools assistantAiUniqueSqlTools;

    @Operation(summary = "mongodb 隔离-普通对话-sys对话提示-chatConfigChatUniqueSqlTools")
    @GetMapping(value = "/chatConfigChatUniqueSqlTools")
    public String chatConfigChatUniqueSqlTools(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int memoryId
    ) {
        return assistantAiUniqueSqlTools.chat(memoryId, message);
    }

    @Operation(summary = "mongodb 隔离-流式对话-sys对话提示-查询数据库-chatConfigChatStreamUniqueSqlTools")
    @GetMapping(value = "/chatConfigChatStreamUniqueSqlTools", produces = "text/stream;charset=UTF-8")
    public Flux<String> chatConfigChatStreamUniqueSqlTools(
            @RequestParam(value = "message", defaultValue = "你是谁") String message
            , @RequestParam(value = "memoryId", defaultValue = "1") int memoryId
    ) {
        TokenStream tokenStream = assistantAiUniqueSqlTools.chatStream(memoryId, message, LocalDate.now().toString());
        Flux<String> returnFlux = Flux.create(sink -> {
            tokenStream.onPartialResponse(s -> {
                        sink.next(s);
                        log.info("===>>>AiConfig.AssistantAi.chatConfigChatStreamUniqueSqlTools response: {}", s);
                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();
        });
        return returnFlux;
    }



}
